'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }

  // 添加身份
  async addUserIds() {
    const { ctx, app } = this;

    const { name } = ctx.request.body;

    // 添加上
    await app.mysql.insert('userids', {
      name,
      auth: '100'
    })

    ctx.body = {
      msg: '添加成功',
      code: 1
    }
  }

  // 获取身份列表
  async getUserIdsList() {
    const { ctx, app } = this;

    const data = await app.mysql.select('userids')

    ctx.body = {
      code: 1,
      data
    }
  }

  // 添加用户
  async getAddUser() {
    const { ctx, app } = this;

    const { username, password, userId } = ctx.request.body;

    // 通过userId 换取身份权限
    const auth = await app.mysql.get('userids', { id: userId })

    // 添加用户
    await app.mysql.insert('userlist', {
      username,
      password,
      auth: auth.auth
    })

    ctx.body = {
      code: 1,
      msg: '添加成功'
    }
  }

  async getList() {
    const { app, ctx } = this;

    const data = await app.mysql.select('userlist')

    ctx.body = {
      code: 1,
      data
    }
  }
}

module.exports = HomeController;
